package com.snopy.socket;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author snopy
 * @version 1.0.0
 * @classname EchoClient
 * @description
 * @email 77912204@qq.com
 * @date 2022/08/01 14:28
 */
@Slf4j
public class EchoClient {
    public static void main(String[] args) {
        if (args.length != 2){
            log.error("Usage: java EchoClient <host name> <port number>");
            System.exit(1);
        }
        String hostName = args[0];
        int portNumber = Integer.parseInt(args[1]);
        try{
            Socket socket = new Socket(hostName, portNumber);
            PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

            String userInput;
            while ((userInput = stdIn.readLine())!=null){
                out.println(userInput);
                log.info("echo:{}",in.readLine());
            }


        }catch (IOException e){
            System.exit(1);
        }
    }
}
